<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>mybatis | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/36.db360c58.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link router-link-active"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link router-link-active"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>java 杂</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/oliver-vuepress/articles/java/other/jdbc.html" class="sidebar-link">JDBC</a></li><li><a href="/oliver-vuepress/articles/java/other/java-load.html" class="sidebar-link">java-load</a></li><li><a href="/oliver-vuepress/articles/java/other/mybatis.html" aria-current="page" class="active sidebar-link">MyBatis</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>mybatis</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">mybatis</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>8/9/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Java</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="什么是orm"><a href="#什么是orm" class="header-anchor">#</a> 什么是ORM？</h2> <p>ORM 全称 Object Relation Mapping: 对象 - 关系映射 的缩写
ORM主要是完成面向对象的编程语言到关系型数据库的映射</p> <h2 id="mybatis-是什么"><a href="#mybatis-是什么" class="header-anchor">#</a> Mybatis 是什么？</h2> <p>Mybatis 是基于 ORM 的 半自动的持久层框架。</p> <ul><li>优点：
<ul><li>不需要去处理JDBC的连接、手动设置参数、以及获取结果集</li> <li>开发  只要关注 核心的 SQL 语句，sql 和 java编码分开，功能边界清洗</li> <li>可以使用 XML 和 注解的方式使用</li> <li>XML 支持 动态标签 SQL</li></ul></li> <li>缺点：
<ul><li>SQL语句依赖于数据库，导致数据库移植性差</li></ul></li></ul> <h2 id="和-的区别是什么"><a href="#和-的区别是什么" class="header-anchor">#</a> <code>#{}</code>和 <code>${}</code> 的区别是什么</h2> <ul><li><code>#{}</code> :  是预编译处理
<ul><li>Mybatis 处理时，会将sql 中  <code>#{}</code>  替换 成 <code>?</code> ,  调用 preparedStatement 的 <code>set</code> 方法进行赋值</li> <li>可以有效的防止 SQL 注入 ::sdasda::</li></ul></li> <li><code>${}</code>:  是字符串替换
<ul><li>Mybatis执行时，直接将字符串替换，没有预编译处</li></ul></li></ul> <h2 id="如何获取自动生成的-主-键值"><a href="#如何获取自动生成的-主-键值" class="header-anchor">#</a> 如何获取⾃动⽣成的(主)键值?</h2> <p>方法1： 使用 useGeneratedKeys + keyProperty 属性
方法2：通过LAST_INSERT_ID()获取刚插⼊记录的⾃增主键值，在insert语句执⾏后，执⾏select （<code>&lt;selectKey /&gt;</code>）LAST_INSERT_ID()就可以获取⾃增主键</p> <h2 id="在mapper中如何传递多个参数"><a href="#在mapper中如何传递多个参数" class="header-anchor">#</a> 在mapper中如何传递多个参数?</h2> <ul><li>在映射⽂件中使⽤<code>#{0}</code>, <code>#{1}</code> 代表传递进来的第⼏个参数</li></ul> <div class="language- extra-class"><pre class="language-text"><code>&gt; #{0}: 代表第一个传入的参数   #{1}: 代表第二个传入的参数
&lt;select id=&quot;selectUser&quot;resultMap=&quot;BaseResultMap&quot;&gt;
select * fromuser_user_t whereuser_name = #{0} anduser_area=#{1} 
&lt;/select&gt;
</code></pre></div><ul><li>使⽤@param注解:来命名参数</li></ul> <div class="language- extra-class"><pre class="language-text"><code>## Dao 层 使用 param 指定变量名

public interface userMapper {
user selectuser(@param(“username”) string username,
@param(“hashedpassword”) string hashedpassword);
}

## XML 层
&lt;select id=”selectuser” resulttype=”user”&gt;
select id, username, hashedpassword
from some_table
where username = #{username}
and hashedpassword = #{hashedpassword}
&lt;/select&gt;

</code></pre></div><ul><li>使用 Map 集合作为参数来装载</li></ul> <h2 id="mybatis动态sql是做什么的-都有哪些动态sql-能简单简单阐述一下动态sql的执行原理"><a href="#mybatis动态sql是做什么的-都有哪些动态sql-能简单简单阐述一下动态sql的执行原理" class="header-anchor">#</a> Mybatis动态sql是做什么的？都有哪些动态sql？能简单简单阐述一下动态sql的执行原理</h2> <ul><li>Mybatis动态sql可以让我们在Xml映射⽂件内，以标签的形式编写动态sql，完成逻辑判断和动态
拼接sql的功能。</li> <li>Mybatis提供了9种动态sql标签：trim|where|set|foreach|if|choose|when|otherwise|bind。</li> <li>其执⾏原理为，使⽤OGNL从sql参数对象中计算表达式的值，根据表达式的值动态拼接sql，以此来完成动态sql的功能。</li></ul> <h2 id="mybatis的xml映射文件中-不同的xml映射文件-id-是否可以重复"><a href="#mybatis的xml映射文件中-不同的xml映射文件-id-是否可以重复" class="header-anchor">#</a> Mybatis的Xml映射⽂件中，不同的Xml映射⽂件，id 是否可以重复</h2> <p>在 同一个Xml 映射文件是不可以重复的，因为我们与Mapper 映射时，使用： <code>namespace+id</code> 做关联的，并存放在 一个 <code>hashMap</code> 中，所以同一个文件时，导致会对应错误，因为映射关系会被覆盖</p> <p>但是在不同的xml隐映射文件 中 id相同，是可以重复的</p> <h2 id="mybatis-架构层级-和-基本的执行流程"><a href="#mybatis-架构层级-和-基本的执行流程" class="header-anchor">#</a> MyBatis 架构层级 和 基本的执行流程</h2> <p>MyBatis分为 三层：</p> <ul><li>API 接口层：
<ul><li>提供的给外部使用的接口API层, 使用API层来操作数据库</li> <li>MyBatis  与 数据库的交互方式
<ul><li>传统的 MyBatis 提供的API</li> <li>使用 Mapper代理方式</li></ul></li></ul></li> <li>数据处理层
<ul><li>参数映射 (ParameterHandler)</li> <li>SQL解析 (SqlSource)</li> <li>SQL执行 (Executor)</li> <li>结果处理 和 映射 (ResultSetHandler)</li></ul></li> <li>基础支持层
<ul><li>连接管理</li> <li>事务管理</li> <li>配置加载</li> <li>缓存处理</li></ul></li></ul> <h3 id="总体执行流程"><a href="#总体执行流程" class="header-anchor">#</a> 总体执行流程</h3> <ul><li>加载配置文件并初始化</li> <li>接收调用请求</li> <li>处理操作请求</li> <li>返回处理结果</li></ul> <img src="/oliver-vuepress/java/other/mybatis/mybatis-1.jpg" alt="foo"> <p>处理过程：</p> <ol><li>根据 SQL 的 ID查找对应的 MappedStatment对象</li> <li>根据传入参数对象解析 MappedStatmentd对象，得到最终要执行的 SQL 和 执行传入参数</li> <li>获取数据库连接，并执行数据库操作，并得到结果</li> <li>根据 MapperdStatement对象的结果映射配置，对返回结果进行转换处理，并得到最终的处理结果</li> <li>释放连接资源</li> <li>返回处理结果</li></ol> <h2 id="mybatis-缓存"><a href="#mybatis-缓存" class="header-anchor">#</a> MyBatis 缓存</h2> <blockquote><p>Mybatis 提供了对象缓存的支持，分为一级缓存 和二级缓存</p></blockquote> <p>相关概念点：</p> <h3 id="一级缓存"><a href="#一级缓存" class="header-anchor">#</a> 一级缓存</h3> <blockquote><p>一级缓存：<code>SqlSession</code>  级别的缓存。在操作数据库时需要构造 <code>SqlSession</code>  对象，在对象中有一个数据结构（HashMap）用于存储缓存数据。不同的 <code>SqlSession</code>   之间的缓存数据区域（HashMap）不影响。<br>
一级缓存具体返回的是：缓存的对象</p></blockquote> <h4 id="一级缓存的底层数据结构"><a href="#一级缓存的底层数据结构" class="header-anchor">#</a> 一级缓存的底层数据结构：</h4> <blockquote><p>底层使用 <code>HashMap</code> 来存储 一级缓存</p></blockquote> <p>HashMap 存储的 <code>cacheKey</code>   和 <code>value</code> :</p> <ul><li>cacheKey :
<ul><li>statementid (namespace 和 方法名 )</li> <li>params （参数）</li> <li>boundSql （mybatis的底层对象，对象中封装这执行的sql对象）</li> <li>rowBounds （mybatis分页对象）</li></ul></li> <li>value :
<ul><li>即查询出的结果</li></ul></li></ul> <p>::注：通过SqlCache中的clearCache 找到其底层存储的结果::</p> <p>clearCache 的调用链路：</p> <img src="/oliver-vuepress/java/other/mybatis/mybatis-2.jpg" alt="foo"> <h4 id="一级缓存是什么时候被创建的-和-一级缓存的执行流程"><a href="#一级缓存是什么时候被创建的-和-一级缓存的执行流程" class="header-anchor">#</a> 一级缓存是什么时候被创建的 和 一级缓存的执行流程：</h4> <p>一级缓存是在执行query 查询时 被创建的：</p> <ul><li>执行查询时，先会调用  <code>createCacheKey</code>  方法获取 cacheKey
<ul><li>具体实现 在BaseExecutor sql执行器中创建的</li></ul></li> <li>优先去一级缓存中去查询，直接返回，无：查询数据库，同时将查询出来的结果存到一级缓存中</li> <li>返回具体数据</li></ul> <h4 id="一级缓存被清除的时机有那些"><a href="#一级缓存被清除的时机有那些" class="header-anchor">#</a> 一级缓存被清除的时机有那些：</h4> <ul><li>做增删改操作，并进行了事务，就会刷新一级缓存</li> <li>sqlSeesion.clearCache(): 手动刷新一级缓存</li> <li>sqlSeesion 被 <code>close</code>  时候</li></ul> <h3 id="二级缓存"><a href="#二级缓存" class="header-anchor">#</a> 二级缓存</h3> <blockquote><p>二级缓存：是mapper(namespace)级别的缓存 即：mapper映射层，多个 <code>SqlSession</code> 去操作同一个Mapper的sql语句，多个SqlSession可以共用二级缓存，二级缓存是跨 <code>SqlSession</code> 的，默认是不开启的<br>
二级缓考 ：它返回时 会将数据重新创建一个对象之后返回 , 而不是直接返回一个对象<br>
二级缓存的基本原理 和 一级缓存基本一致</p></blockquote> <p>::注意事项::</p> <p>开启后二级缓存后，所有的mybaits 的pojo 都需要继承 Serializabl序列化接口
因为二级缓存可能存在 内存 或者硬盘 等地方 ，需要进行 序列化 和 反序列化</p> <h4 id="二级缓存的开启方式"><a href="#二级缓存的开启方式" class="header-anchor">#</a> 二级缓存的开启方式</h4> <ol><li>XML 形式
sqlMapConfig.xml 配置文件</li></ol> <div class="language- extra-class"><pre class="language-text"><code>&lt;settings&gt;
    &lt;setting name=“cacheEnable” value=“true”/&gt;
&lt;/settings&gt;
</code></pre></div><p>mapper.xml 文件</p> <div class="language- extra-class"><pre class="language-text"><code>&lt;cache&gt;&lt;/cache&gt;
</code></pre></div><ol start="2"><li>注解形式
在Mapper 类上 添加 <code>@CacheNamesapce</code> 注解</li></ol> <h4 id="二级缓存的实现分布式缓存的方式"><a href="#二级缓存的实现分布式缓存的方式" class="header-anchor">#</a> 二级缓存的实现分布式缓存的方式</h4> <blockquote><p>自带的二级缓存 无法用于分布式缓存 因为不同不服务器内存 不通用，所以需要使用不同的第三方缓存组件来实现、redis、memcached 、ehcahce</p></blockquote> <p>通过注解来指定使用缓存类 <code>@CacheNamesapce(implements:PerpetualCache.class)</code></p> <h2 id="mybatis的插件运行原理-以及如何编写一个插件"><a href="#mybatis的插件运行原理-以及如何编写一个插件" class="header-anchor">#</a> Mybatis的插件运行原理，以及如何编写一个插件？</h2> <blockquote><p>对 mybatis 来说插件就是拦截器，用来增强核心对象的功能，增强功能本质是借助于底层的动态代理实现的，换句话说 mybatis中的四大对象都是代理对象<br>
Mybatis 四大核心组件 <code>Executor</code>、<code>StatementHandler</code>、<code>ParameterHandler</code>、<code>ResultSetHandler</code></p></blockquote> <h3 id="运行原理"><a href="#运行原理" class="header-anchor">#</a> 运行原理</h3> <ol><li>每个创建出来的对象不是直接返回的，而是interceptorChain.pluginAll(parameterHandler)；</li> <li>获取到所有的Interceptor(拦截器)（插件需要实现的接口）；调用interceptor.plugin(target);返回target包装后的对象</li> <li>插件机制，我们可以使用插件为目标对象创建一个代理对象；aop（面向切面）我们的插件可以以为四大对象创建出代理对象，代理对象就可以拦截到四大对象的每一个执行；</li></ol> <h3 id="如何编写一个插件"><a href="#如何编写一个插件" class="header-anchor">#</a> 如何编写一个插件</h3> <ul><li>我们需要实现 实现MyBatis的Interceptor  (拦截器)接⼝并复写intercept()⽅法
<ul><li>intercept() :  插件核心方法</li> <li>plugin(): 生成target的代理对象</li> <li>setProperties(): 传递插件所需要的参数</li></ul></li> <li>需要给插件使用注解 <code>@Intercepts</code>  、<code>@Signature</code> ，指定相关参数
<ul><li>type: 需要拦截的类</li> <li>method:  需要拦截的方法</li> <li>args:  方法入参</li></ul></li> <li>在配置⽂件中配置你编写的插件 <code>sqlMapConfig.xml</code></li></ul> <h2 id="mybatis是如何进行分⻚的-分⻚插件的原理是什么"><a href="#mybatis是如何进行分⻚的-分⻚插件的原理是什么" class="header-anchor">#</a> Mybatis是如何进⾏分⻚的？分⻚插件的原理是什么</h2> <ul><li>Mybatis使⽤RowBounds对象进⾏分⻚，它是针对ResultSet结果集执⾏的内存分⻚，</li> <li>如果需要物理分页的话
<ul><li>在sql内直接书写带有物理分⻚的参数来完成物理分⻚功能</li> <li>使用分页插件来实现物理分页</li></ul></li></ul> <blockquote><p>分页插件 同前的插件解释的一样：使⽤JDK的动态代理，为需要拦截的接⼝⽣成代理对象以实现接⼝⽅法拦截功能，（具体可以查看插件相关知识）</p></blockquote> <h2 id="mybatis是否支持延迟加载-如果支持-它的实现原理"><a href="#mybatis是否支持延迟加载-如果支持-它的实现原理" class="header-anchor">#</a> Mybatis是否⽀持延迟加载？如果⽀持，它的实现原理</h2> <blockquote><p>延迟加载：即需要用到数据的时候才进行加载，不需要的时候不进行加载，延迟加载，也叫懒加载</p></blockquote> <img src="/oliver-vuepress/java/other/mybatis/mybatis-3.jpg" alt="foo"> <p>::注：:: MyBatis 使用 <code>CGLIB</code> 或 <code>javassist</code>（默认）创建目标对象的代理对象</p> <p>Mybatis  是 支持延时加载的</p> <ul><li>可以配置文件中配置： lazyLoadingEnabled ： true 进行全局开启</li> <li>也可以 <code>&lt;collection&gt;</code> 标签中 使用 <code>fetchType=&quot;lazy</code> 属性 进行局部开启</li></ul> <h3 id="原理"><a href="#原理" class="header-anchor">#</a> 原理：</h3> <ul><li>Mysql 开启 延时加载后</li> <li>MyBatis 返回查询结构对象时，会对ResultMap 中的属性进行判断， 是否有 <code>Lazy</code> 属性 ，有则会使用 <code>CGLIB</code> 或 <code>javassist</code>（默认）创建目标对象的代理对象</li> <li>当调用目标方法时，会进入拦截器 调用 <code>invoke()</code> 方法</li> <li>会判断 你当前获取的属性，是不是延时加载属性，如果是，那么就会单独发送事先保存好的查询关联B对象的sql，进行延时加载</li></ul> <h3 id="详细源码"><a href="#详细源码" class="header-anchor">#</a> 详细源码：</h3> <ol><li>DefaultResultSetHandler.class</li></ol> <div class="language-java extra-class"><pre class="language-java"><code><span class="token comment">// 创建结构对象</span>
<span class="token keyword">private</span> <span class="token class-name">Object</span> <span class="token function">createResultObject</span><span class="token punctuation">(</span><span class="token class-name">ResultSetWrapper</span> rsw<span class="token punctuation">,</span> <span class="token class-name">ResultMap</span> resultMap<span class="token punctuation">,</span> <span class="token class-name">ResultLoaderMap</span> lazyLoader<span class="token punctuation">,</span> <span class="token class-name">String</span> columnPrefix<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>useConstructorMappings <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token comment">// reset previous mapping result</span>
  <span class="token keyword">final</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Class</span><span class="token punctuation">&lt;</span><span class="token operator">?</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> constructorArgTypes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">final</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> constructorArgs <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 创建返回的结果映射的真实对象</span>
  <span class="token class-name">Object</span> resultObject <span class="token operator">=</span> <span class="token function">createResultObject</span><span class="token punctuation">(</span>rsw<span class="token punctuation">,</span> resultMap<span class="token punctuation">,</span> constructorArgTypes<span class="token punctuation">,</span> constructorArgs<span class="token punctuation">,</span> columnPrefix<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>resultObject <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span><span class="token function">hasTypeHandlerForResultObject</span><span class="token punctuation">(</span>rsw<span class="token punctuation">,</span> resultMap<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">final</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ResultMapping</span><span class="token punctuation">&gt;</span></span> propertyMappings <span class="token operator">=</span> resultMap<span class="token punctuation">.</span><span class="token function">getPropertyResultMappings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">ResultMapping</span> propertyMapping <span class="token operator">:</span> propertyMappings<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// issue gcode #109 &amp;&amp; issue #149</span>
		<span class="token comment">// 判断属性有没配置嵌套查询，如果有就创建代理对象 (resultMap 中的属性)</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>propertyMapping<span class="token punctuation">.</span><span class="token function">getNestedQueryId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> propertyMapping<span class="token punctuation">.</span><span class="token function">isLazy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        resultObject <span class="token operator">=</span> configuration<span class="token punctuation">.</span><span class="token function">getProxyFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">createProxy</span><span class="token punctuation">(</span>resultObject<span class="token punctuation">,</span> lazyLoader<span class="token punctuation">,</span> configuration<span class="token punctuation">,</span> objectFactory<span class="token punctuation">,</span> constructorArgTypes<span class="token punctuation">,</span> constructorArgs<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>useConstructorMappings <span class="token operator">=</span> resultObject <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>constructorArgTypes<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// set current mapping result</span>
  <span class="token keyword">return</span> resultObject<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="2"><li>JavassistProxyFactory.class  内部类 EnhancedResultObjectProxyImpl</li></ol> <div class="language-java extra-class"><pre class="language-java"><code>
  <span class="token annotation punctuation">@Override</span>
  <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">Object</span> enhanced<span class="token punctuation">,</span> <span class="token class-name">Method</span> method<span class="token punctuation">,</span> <span class="token class-name">Method</span> methodProxy<span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span>
    <span class="token keyword">final</span> <span class="token class-name">String</span> methodName <span class="token operator">=</span> method<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
      <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lazyLoader<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>WRITE_REPLACE_METHOD<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token class-name">Object</span> original<span class="token punctuation">;</span>
          <span class="token keyword">if</span> <span class="token punctuation">(</span>constructorArgTypes<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            original <span class="token operator">=</span> objectFactory<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            original <span class="token operator">=</span> objectFactory<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> constructorArgTypes<span class="token punctuation">,</span> constructorArgs<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span>
          <span class="token class-name">PropertyCopier</span><span class="token punctuation">.</span><span class="token function">copyBeanProperties</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> enhanced<span class="token punctuation">,</span> original<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token keyword">if</span> <span class="token punctuation">(</span>lazyLoader<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">JavassistSerialStateHolder</span><span class="token punctuation">(</span>original<span class="token punctuation">,</span> lazyLoader<span class="token punctuation">.</span><span class="token function">getProperties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> objectFactory<span class="token punctuation">,</span> constructorArgTypes<span class="token punctuation">,</span> constructorArgs<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> original<span class="token punctuation">;</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
				<span class="token comment">//延迟加载数量⼤于0</span>
          <span class="token keyword">if</span> <span class="token punctuation">(</span>lazyLoader<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>FINALIZE_METHOD<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token comment">// ⼀次加载性所有需要要延迟加载属性或者包含触发延迟加载⽅法</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>aggressive <span class="token operator">||</span> lazyLoadTriggerMethods<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              lazyLoader<span class="token punctuation">.</span><span class="token function">loadAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token comment">//判断是否为set⽅法，set⽅法不需要延迟加载</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">PropertyNamer</span><span class="token punctuation">.</span><span class="token function">isSetter</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token keyword">final</span> <span class="token class-name">String</span> property <span class="token operator">=</span> <span class="token class-name">PropertyNamer</span><span class="token punctuation">.</span><span class="token function">methodToProperty</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">;</span>
              lazyLoader<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>property<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token comment">//判断是否为get⽅法，get的属性是不是个需要延时加载的属性</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">PropertyNamer</span><span class="token punctuation">.</span><span class="token function">isGetter</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token keyword">final</span> <span class="token class-name">String</span> property <span class="token operator">=</span> <span class="token class-name">PropertyNamer</span><span class="token punctuation">.</span><span class="token function">methodToProperty</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token comment">//延迟加载单个属性</span>
              <span class="token keyword">if</span> <span class="token punctuation">(</span>lazyLoader<span class="token punctuation">.</span><span class="token function">hasLoader</span><span class="token punctuation">(</span>property<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                lazyLoader<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>property<span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">return</span> methodProxy<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>enhanced<span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Throwable</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">throw</span> <span class="token class-name">ExceptionUtil</span><span class="token punctuation">.</span><span class="token function">unwrapThrowable</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><blockquote><p>总结来说：就是 代理对象，调用延时加载属性的 方法时，就会进入拦截器方法的 invoke() 方法，判断属性是否是个延时加载属性，如果时则执行对应的sql，进行返回结果</p></blockquote> <h2 id="mybatis都有哪些executor执行器-它们之间的区别"><a href="#mybatis都有哪些executor执行器-它们之间的区别" class="header-anchor">#</a> Mybatis都有哪些Executor执⾏器？它们之间的区别</h2> <p>Executor：执行器是一个接口，他有三个常用的实现类：</p> <ul><li>SimpleExecutor: 普通的执行器
<ul><li>SimpleExecutor是执行器的默认实现，主要完成了“执行”功能，在利用StatementHandler 完成。每次调用执行方法 都会构建一个StatementHandler，并预设参数</li> <li>即：每执⾏⼀次update或select，就开启⼀个Statement对象，⽤完⽴刻关闭Statement对象。</li></ul></li></ul> <ul><li>ReuseExecutor: 可重用执行器
<ul><li>可重复使用JDBC中Statement，减少预编译次数。该执行器在执行SQL时会把Statement缓存起来，如果下次碰到相同的SQL，就会取出来使用。</li></ul></li></ul> <ul><li>BatchExecutor: 批量操作的执行器
<ul><li>每次的执行操作 不会立即执行，而是把对应的Statement 填充好参数之后暂存起来。当调用 flushStatements 的时候一次性提交到数据库。它可用于批处理插入的场景。效果等同于SQL语句的拼装。</li></ul></li></ul> <blockquote><p>三个实现类，还有一个父类：<code>BaseExecutor</code></p></blockquote> <img src="/oliver-vuepress/java/other/mybatis/mybatis-4.jpg" alt="foo"> <h2 id="mybatis-的-dao接口的工作原理-dao接口里的方法是否支持重载"><a href="#mybatis-的-dao接口的工作原理-dao接口里的方法是否支持重载" class="header-anchor">#</a> Mybatis 的 Dao接口的工作原理？Dao接口里的方法是否支持重载?</h2> <ul><li>Dao接⼝，就是⼈们常说的Mapper接⼝，接⼝的全限名，就是映射xml⽂件中的namespace的值， 接⼝的⽅法名，就是映射⽂件中MappedStatement的id值，接⼝⽅法内的参数，就是传递给sql的参数。</li> <li>Mapper接⼝是没有实现类的，当调⽤接⼝⽅法时，接⼝全限名+⽅法名拼接字符串作为key值，可唯⼀定位⼀个MappedStatement</li></ul> <blockquote><p>Dao接⼝的⼯作原理是JDK动态代理，Mybatis运⾏时会使⽤JDK动态代理为Dao接⼝⽣成代理proxy对象，代理对象proxy会拦截接⼝⽅法，从⽽执⾏MappedStatement所代表的sql，然后将sql执⾏结果返回。</p></blockquote> <h3 id="dao接口里的方法是否支持重载"><a href="#dao接口里的方法是否支持重载" class="header-anchor">#</a> Dao接口里的方法是否支持重载?</h3> <blockquote><p>Dao接⼝⾥的⽅法，是不能重载的，因为是全限名+⽅法名的保存和寻找策略。</p></blockquote> <p>#草稿箱/待整理的学习资料</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/oliver-vuepress/articles/java/other/java-load.html" class="prev">
            java-load
          </a></span> <!----></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#什么是orm" class="sidebar-link reco-side-什么是orm" data-v-cb1513f6>什么是ORM？</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis-是什么" class="sidebar-link reco-side-mybatis-是什么" data-v-cb1513f6>Mybatis 是什么？</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#和-的区别是什么" class="sidebar-link reco-side-和-的区别是什么" data-v-cb1513f6>#{}和 ${} 的区别是什么</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#如何获取自动生成的-主-键值" class="sidebar-link reco-side-如何获取自动生成的-主-键值" data-v-cb1513f6>如何获取⾃动⽣成的(主)键值?</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#在mapper中如何传递多个参数" class="sidebar-link reco-side-在mapper中如何传递多个参数" data-v-cb1513f6>在mapper中如何传递多个参数?</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis动态sql是做什么的-都有哪些动态sql-能简单简单阐述一下动态sql的执行原理" class="sidebar-link reco-side-mybatis动态sql是做什么的-都有哪些动态sql-能简单简单阐述一下动态sql的执行原理" data-v-cb1513f6>Mybatis动态sql是做什么的？都有哪些动态sql？能简单简单阐述一下动态sql的执行原理</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis的xml映射文件中-不同的xml映射文件-id-是否可以重复" class="sidebar-link reco-side-mybatis的xml映射文件中-不同的xml映射文件-id-是否可以重复" data-v-cb1513f6>Mybatis的Xml映射⽂件中，不同的Xml映射⽂件，id 是否可以重复</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis-架构层级-和-基本的执行流程" class="sidebar-link reco-side-mybatis-架构层级-和-基本的执行流程" data-v-cb1513f6>MyBatis 架构层级 和 基本的执行流程</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#总体执行流程" class="sidebar-link reco-side-总体执行流程" data-v-cb1513f6>总体执行流程</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis-缓存" class="sidebar-link reco-side-mybatis-缓存" data-v-cb1513f6>MyBatis 缓存</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#一级缓存" class="sidebar-link reco-side-一级缓存" data-v-cb1513f6>一级缓存</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#二级缓存" class="sidebar-link reco-side-二级缓存" data-v-cb1513f6>二级缓存</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis的插件运行原理-以及如何编写一个插件" class="sidebar-link reco-side-mybatis的插件运行原理-以及如何编写一个插件" data-v-cb1513f6>Mybatis的插件运行原理，以及如何编写一个插件？</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#运行原理" class="sidebar-link reco-side-运行原理" data-v-cb1513f6>运行原理</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#如何编写一个插件" class="sidebar-link reco-side-如何编写一个插件" data-v-cb1513f6>如何编写一个插件</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis是如何进行分⻚的-分⻚插件的原理是什么" class="sidebar-link reco-side-mybatis是如何进行分⻚的-分⻚插件的原理是什么" data-v-cb1513f6>Mybatis是如何进⾏分⻚的？分⻚插件的原理是什么</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis是否支持延迟加载-如果支持-它的实现原理" class="sidebar-link reco-side-mybatis是否支持延迟加载-如果支持-它的实现原理" data-v-cb1513f6>Mybatis是否⽀持延迟加载？如果⽀持，它的实现原理</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#原理" class="sidebar-link reco-side-原理" data-v-cb1513f6>原理：</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#详细源码" class="sidebar-link reco-side-详细源码" data-v-cb1513f6>详细源码：</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis都有哪些executor执行器-它们之间的区别" class="sidebar-link reco-side-mybatis都有哪些executor执行器-它们之间的区别" data-v-cb1513f6>Mybatis都有哪些Executor执⾏器？它们之间的区别</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#mybatis-的-dao接口的工作原理-dao接口里的方法是否支持重载" class="sidebar-link reco-side-mybatis-的-dao接口的工作原理-dao接口里的方法是否支持重载" data-v-cb1513f6>Mybatis 的 Dao接口的工作原理？Dao接口里的方法是否支持重载?</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/other/mybatis.html#dao接口里的方法是否支持重载" class="sidebar-link reco-side-dao接口里的方法是否支持重载" data-v-cb1513f6>Dao接口里的方法是否支持重载?</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/36.db360c58.js" defer></script>
  </body>
</html>
